/**
 * 
 */
$.extend( $.fn.dataTable.defaults, {
	serverSide: true, //打开后台分页  
    searching: false,
    ordering:  false,
    processing: true, //打开数据加载时的等待效果
	iDisplayStart: oldStart,
	pageLength: oldLength,
	lengthMenu: [[20, 50, 100, 999999], [20, 50, 100, "All"]],
	//pagingType: "full",
	scrollCollapse: true, //当显示更少的记录时，是否允许表格减少高度
	dom: 'rt<"row"<"col-xs-12"<"col-sm-2"l><"col-sm-4"i><"col-sm-6"p>>>',
	language: { 
		 emptyTable: '没有数据', 
		 search: '检索:', 
		 lengthMenu: '每页 _MENU_ 条', 
		 zeroRecords: '没有数据', 
		 paginate: { 
		     first:      '第一页',
		     last:       '最后一页', 
		     previous:   '前一页',
		     next:       '后一页' 
		 }, 
		 info: ' 共_TOTAL_ 条数据，当前_START_ - _END_ ', 
		 infoEmpty: '没有数据', 
		 infoFiltered: '(过滤总件数 _MAX_ 条)' ,
		 loadingRecords: "loading...", 
		 processing:     "processing..."
	} ,
	ajax: {
			url: ctlKey+"/pagination",
		 	data : function(d){
		 		jQuery.extend(d, $("#searchForm").serializeJSON());
		 		return d;
			 	}
	},
	drawCallback:function(settings ){
		// var api = this.api();
	        // 输出当前页的数据到浏览器控制台
	       // console.log( api.rows( {page:'current'} ) );
		//console.log($(this));
		// console.log(api.page());
	}
});
$(function(){
	//搜索
	$("#searchBtn").click(function(){
		bootbox.dialog({
			title : " <i class='ace-icon fa fa-search bigger-120'></i>Search",
			//message : $("#searchBox").children().clone(),
			message : function(){
				var box = $("#searchBox").children().clone();
				try {
					searchBoxInit(box);
				} catch (e) {
					// TODO: handle exception
				}
				return box;
			},
			buttons: {
				go: {
					label: "OK",
					className: "btn-primary btn-sm",
					callback: function(){
						//元素还原
						$("#searchBox").empty().append($(this).find(".bootbox-body"));
						tables.draw();
					}
				},
				cancel: {
					label: "Cancel",
					className: "btn-sm",
					callback: function(){
					}
				}
			}
		});
	});
	//展开详情，  需要单独写 formatDetail 方法
	$('#dataTable tbody').on('click', '.show-details-btn', function (e) {
        $(this).find("i").toggleClass('fa-angle-double-down').toggleClass('fa-angle-double-up');
		e.preventDefault();
        var tr = $(this).closest('tr');
        var row = tables.row( tr );
        if ( row.child.isShown() ) {
            // This row is already open - close it
            row.child.hide();
        }
        else {
            // Open this row
            row.child( formatDetail(row.data()) ).show();
        }
    } );
	
	//全选功能
	$("th input[type=checkbox]").change(function(){
		$("tr input[type=checkbox]").prop("checked",$(this).prop("checked"));
	});
	//重绘时还原复选框
	$('#dataTable').on( 'draw.dt', function () {
		$("th input[type=checkbox]").prop("checked", false);
	} );
});

$("#addBtn").click(function(){
	location.href=ctlKey +"/add";
});

function delRow(id){
	bootbox.confirm("Are you sure ?",function(r){
		if(r) {
			$.post(ctlKey + "/delete/" + id, function(data){
				if(data.success) {
					tables.draw(false);
				} else {
					bootbox.dialog({
						title: '出错了',
						message: '<p><i class="fa fa-bomb"></i>' + data.message + '</p>'
					});
				}
			},"json");
		}
	});
}

$("#batchDelBtn").click(function(){
	var chk_value =[];    
	  $('input[name="ids"]:checked').each(function(){    
	   chk_value.push($(this).val());    
	  });  
	  if(chk_value.length==0) {
		  bootbox.alert('Please select rows!');
	  }else{
		  bootbox.confirm("Are you sure ?",function(r){
			  if(r) {
				  $.post(ctlKey + "/betchDelete?ids=" + chk_value, function(data){
					  if (data.success) {
						  tables.draw(false);
					  } else {
						  bootbox.dialog({
							  title: '出错了',
							  message: '<p><i class="fa fa-bomb"></i>' + msg + '</p>'
						  });
					  }
				  });
			  }
		  });
	  }
});
